<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="学术活动名称" prop="activityName">
        <el-input v-model="formData.activityName" placeholder="请输入学术活动名称" />
      </el-form-item>
      <el-form-item label="活动级别" prop="activityLevel">
        <el-select v-model="formData.activityLevel" placeholder="请输入活动级别">
          <el-option
            v-for="dict in getIntDictOptions(DICT_TYPE.LEVEL)"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="参加学生人数" prop="peopleNumber">
        <el-input v-model="formData.peopleNumber" placeholder="请输入参加学生人数" />
      </el-form-item>
      <el-form-item label="学生姓名" prop="studentName">
        <el-input v-model="formData.studentName" type="textarea" placeholder="请输入学生姓名" />
      </el-form-item>
      <el-form-item label="专业班级" prop="majorClass">
        <el-input v-model="formData.majorClass" type="textarea" placeholder="请输入专业班级" />
      </el-form-item>
      <el-form-item label="指导教师" prop="instructor">
        <el-input v-model="formData.instructor" placeholder="请输入指导教师" />
      </el-form-item>
      <el-form-item label="参加时间" prop="awardDate">
        <el-date-picker
          v-model="formData.awardDate"
          type="date"
          value-format="x"
          placeholder="选择参加时间（年月）"
        />
      </el-form-item>
      <el-form-item label="备注" prop="remark">
        <el-input v-model="formData.remark" type="textarea" placeholder="请输入备注" />
      </el-form-item>
      <el-row>
        <el-col :span="24">
          <p style="font-size: 14px; line-height: 1.6; color: #333">
            注:
            <br />
            1. 统计时间为2020年9月1日至2021年8月31日。
            <br />
            2. “活动级别”填国家级、省部级、市级、校级。
            <br />
            3. 多人参加的活动，姓名和班级请用分号隔开。
          </p>
        </el-col>
      </el-row>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { DICT_TYPE, getIntDictOptions } from '@/utils/dict'
import * as ActivityApi from '@/api/data/activity'

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  id: undefined,
  activityName: undefined,
  activityLevel: undefined,
  peopleNumber: undefined,
  studentName: undefined,
  majorClass: undefined,
  instructor: undefined,
  awardDate: undefined,
  remark: undefined,
  status: undefined,
  reason: undefined
})
const formRules = reactive({
  activityName: [{ required: true, message: '学术活动名称不能为空', trigger: 'blur' }],
  activityLevel: [{ required: true, message: '活动级别不能为空', trigger: 'blur' }],
  peopleNumber: [{ required: true, message: '参加学生人数不能为空', trigger: 'blur' }],
  studentName: [{ required: true, message: '学生姓名不能为空', trigger: 'blur' }],
  majorClass: [{ required: true, message: '专业班级不能为空', trigger: 'blur' }],
  instructor: [{ required: true, message: '指导教师不能为空', trigger: 'blur' }],
  awardDate: [{ required: true, message: '参加时间（年月）不能为空', trigger: 'blur' }],
  status: [{ required: true, message: '状态 0 未通过 1 审核中 2 通过不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await ActivityApi.getActivity(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  if (!formRef) return
  const valid = await formRef.value.validate()
  if (!valid) return
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as ActivityApi.ActivityVO
    if (formType.value === 'create') {
      await ActivityApi.createActivity(data)
      message.success(t('common.createSuccess'))
    } else {
      await ActivityApi.updateActivity(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    activityName: undefined,
    activityLevel: undefined,
    peopleNumber: undefined,
    studentName: undefined,
    majorClass: undefined,
    instructor: undefined,
    awardDate: undefined,
    remark: undefined,
    status: undefined,
    reason: undefined
  }
  formRef.value?.resetFields()
}
</script>
